package com.zzptc.tkfate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.zzptc.tkfate.entity.Cars;
import com.zzptc.tkfate.entity.CheliangPinpai;
import com.zzptc.tkfate.service.CarsService;
import com.zzptc.tkfate.service.CheliangPinpaiServic;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //getAllCarsInfo();
        getCheliangPinpai();
    }

    private void getCheliangPinpai() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheliangPinpaiServic cheliangPinpaiServic = retrofit.create(CheliangPinpaiServic.class);
        Call<CheliangPinpai> call = cheliangPinpaiServic.getAllPinpai("79729168b7661c89f1f4e56936121f31", "A");
        call.enqueue(new Callback<CheliangPinpai>() {
            @Override
            public void onResponse(Call<CheliangPinpai> call, Response<CheliangPinpai> response) {
                CheliangPinpai ch=response.body();
                Log.d(TAG, "onResponse: "+ ch.getResult().get(0).getBrand_name());
            }

            @Override
            public void onFailure(Call<CheliangPinpai> call, Throwable t) {

            }
        });
    }

    private void getAllCarsInfo() {
        //创建retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建接口对象
        CarsService carsService=retrofit.create(CarsService.class);
        //调用接口中的抽象方法
        Call<Cars> allCars = carsService.getAllCars();
        //调用Call类型中的异步方法
        allCars.enqueue(new Callback<Cars>() {
            @Override
            public void onResponse(Call<Cars> call, Response<Cars> response) {
                Cars cars=response.body();

                Log.d(TAG,"onResponse:"+new Gson().toJson(cars));
            }

            @Override
            public void onFailure(Call<Cars> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });
    }
}
